package com.sibrary.components;

import com.sibrary.utils.Constants;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class CustomWebView extends WebView {

	private boolean mIsLoading;
	private int mProgress;

	public CustomWebView(Context context) {
		super(context);

		initWebViewOptions();
		mIsLoading = false;
		mProgress = 0;
	}

	public CustomWebView(Context context, AttributeSet attrs) {
		super(context, attrs);

		initWebViewOptions();
		mIsLoading = false;
		mProgress = 0;
	}

	private void initWebViewOptions() {
		WebSettings settings = getSettings();

		settings.setJavaScriptEnabled(Constants.mSharedPreferences.getBoolean(
				Constants.PREFERENCE_KEY_ENABLE_JAVASCRIPT, true));
		
		settings.setLoadsImagesAutomatically(Constants.mSharedPreferences.getBoolean(
				Constants.PREFERENCE_KEY_ENABLE_LOAD_IMAGE, true));

		settings.setSupportMultipleWindows(true);
	}

	/**
	 * Triggered when a new page loading is requested.
	 */
	public void notifyPageStarted() {
		mIsLoading = true;
	}

	/**
	 * Triggered when the page has finished loading.
	 */
	public void notifyPageFinished() {
		mIsLoading = false;
		mProgress = 100;
	}

	/**
	 * 查看是否正在载入网页
	 * 
	 * @return
	 */
	public boolean isPageLoading() {

		if (mIsLoading && (mProgress > 0 && mProgress < 100)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 设置网页载入进度
	 * 
	 * @param progress
	 */
	public void setProgress(int progress) {
		mProgress = progress;
	}

	/**
	 * 获取载入网页进度
	 * 
	 * @return
	 */
	public int getProgress() {
		return mProgress;
	}
}
